import { defineStore } from 'pinia'
import api from '@/api'
let aLiInterval = null
export const useSystemStore = defineStore('system', {
  state: () => {
    return {
      // 0 t 1 kg
      displayWeightType: '',
      aliOssSignature: {
        policy: '',
        OSSAccessKeyId: '',
        signature: '',
        expire: 0
      },
      defaultMaterialSh: {
        warehouseId: '',
        warehouseName: '',
        storehouseId: '',
        storehouseName: '',
        materialType: '',
        materialId: '',
        materialNo: '',
        materialName: '',
        materialProperty: ''
      },
      defaultMaterialFh: {
        warehouseId: '',
        warehouseName: '',
        storehouseId: '',
        storehouseName: '',
        materialType: '',
        materialId: '',
        materialNo: '',
        materialName: '',
        materialProperty: ''
      },
      materialInited: false
    }
  },
  actions: {
    async getAliOssSignature() {
      const res = await api.getAliOssSignature()
      this.aliOssSignature = res.data
      if (aLiInterval === null) {
        aLiInterval = setInterval(this.getAliOssSignature, 1 * 60 * 60 * 1000)
      }
    },
    async getDisplayWeightType(companyId) {
      const res = await api.systemConfig({
        companyId,
        code: 'DISPLAY_WEIGHT_TYPE'
      })
      this.displayWeightType = res.data.value + ''
    },
    async getDefaultMaterial(companyId) {
      const [fh, sh] = await Promise.all([
        api.systemConfig({
          companyId,
          code: 'DEFAULT_FH_CONFIG'
        }),
        api.systemConfig({
          companyId,
          code: 'DEFAULT_SH_CONFIG'
        })
      ])
      this.defaultMaterialSh = JSON.parse(sh.data.name)
      this.defaultMaterialFh = JSON.parse(fh.data.name)
      this.materialInited = true
    }
  }
})
